FogHelper = {}
FogHelper.__index = FogHelper
 
function FogHelper.new()
    local instance = {

    }
    
    setmetatable(instance, FogHelper)
    return instance
end

function FogHelper:checkIfFogNeedShown()
  if(currentTurn % 5 == 4) then
    return true
  elseif(currentTurn % 5 == 1) then
    return false
  end
end

function FogHelper:showAllFog()
  for k, v in pairs(nightShadeList) do
    v:setVisible(true)
  end
end

function FogHelper:hideAllFog()
  for k, v in pairs(nightShadeList) do
    v:setVisible(false)
  end
  
  for kk, vv in pairs(pl1:getOwnedBuildingsList()) do
    if vv:getCaptureTextbox() ~= nil then
      vv:getCaptureTextbox():setVisible(true)
    end
  end
  for kk, vv in pairs(pl1:getOwnedUnitsList()) do
    vv:getHealthBox():setVisible(true)
    vv:attackReset()
  end
  
  for kk, vv in pairs(pl2:getOwnedBuildingsList()) do
    if vv:getCaptureTextbox() ~= nil then
      vv:getCaptureTextbox():setVisible(true)
    end
  end
  for kk, vv in pairs(pl2:getOwnedUnitsList()) do
    vv:getHealthBox():setVisible(true)
    vv:attackReset()
  end
  
  for k, v in pairs(captureBuildingsList) do
    local building = v[2]
    if building:getCaptureTextbox() ~= nil then
      building:getCaptureTextbox():setVisible(true)
    end
  end
end

function FogHelper:hideFogForProp(x1, y1, x2, y2, hideOrShow)
  local centerFog = self:getNightProp(x2, y2)
  if centerFog ~= nil then centerFog:setVisible(hideOrShow) end

  local rightX = x2 + 32
  local rightY = y2
  local leftX = x1 - 32
  local leftY = y2
  local upX = x2
  local upY =  y2 + 48
  local downX = x2
  local downY = y1
  
  local rightFog = self:getNightProp(rightX, rightY)
  local leftFog = self:getNightProp(leftX, leftY)
  local upFog = self:getNightProp(upX, upY)
  local downFog = self:getNightProp(downX, downY)
  
  if rightFog ~= nil then 
    rightFog:setVisible(hideOrShow) 
  end
  if leftFog ~= nil then 
    leftFog:setVisible(hideOrShow) 
  end
  if upFog ~= nil then 
    upFog:setVisible(hideOrShow) 
  end
  if downFog ~= nil then 
    downFog:setVisible(hideOrShow) 
  end
end

function FogHelper:hideFogForCurrentPlayer()
  if currentPlayerTurn == pl1 then
    self:hideOrShowForPlayer(pl2, true)
    self:hideOrShowForPlayer(pl1, false)
  else
    self:hideOrShowForPlayer(pl1, true)
    self:hideOrShowForPlayer(pl2, false)
  end
end

function FogHelper:hideOrShowForPlayer(player, hideOrShow)
  for k, v in pairs(captureBuildingsList) do
    local unit = v[1]
    local building = v[2]
    local _type = building:getType()

    if _type == "Castle-neutral" or 
       _type == "BarracksCavalry-neutral" or 
       _type == "BarracksMelee-neutral" or 
       _type == "BarracksRange-neutral" or 
       _type == "CapturePointBuilding-neutral" then
      
      if currentPlayerTurn ~= unit:getOwnerObject() then
        building:getCaptureTextbox():setVisible(false)
      else
        building:getCaptureTextbox():setVisible(true)
      end
    end
  end
  
  for kk, vv in pairs(player:getOwnedBuildingsList()) do
    if vv:getCaptureTextbox() ~= nil then
      vv:getCaptureTextbox():setVisible(not hideOrShow)
    end
    local tile = vv:getTileWhichPlacedOn()
    local fog = tile:getFogObject()
    local x1, y1, x2, y2 = fog:getRect()
    self:hideFogForProp(x1, y1, x2, y2, hideOrShow)
  end
  
  for kk, vv in pairs(player:getOwnedUnitsList()) do
    local tile = vv:getTileUnitOn()
    vv:getHealthBox():setVisible(not hideOrShow)
    local x1, y1, x2, y2 = vv:getRectangleValues()
    self:hideFogForProp(x1, y1, x2, y2, hideOrShow)
  end
end

function FogHelper:getNightProp(x,y)
  
  local iX, iY = MainLayer:wndToWorld(x, y)
  local props = {gamePartition:propListForPoint(iX, -iY, 0, MOAILayer.SORT_PRIORITY_DESCENDING)}
  
  for pk, pv in pairs(props) do
    for k,v in pairs(nightShadeList) do
      if pv == v then
        return v
      end
    end
  end
end

return FogHelper